﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Happy.ExtentionMethods
{
    /// <summary>
    /// 扩展了<see cref="object"/>。
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// 浅拷贝对象到一个字典，如果对象为null，就返回空字典。
        /// </summary>
        public static Dictionary<string, object> ToDictionary(this object that, params string[] excludeProperties)
        {
            if (excludeProperties == null)
            {
                excludeProperties = new string[0];
            }

            var dic = new Dictionary<string, object>();

            if (that == null)
            {
                return dic;
            }

            foreach (var property in that.GetType().GetProperties())
            {
                if (property.CanRead && !excludeProperties.Contains(property.Name))
                {
                    dic[property.Name] = property.GetValue(that, null);
                }
            }

            return dic;
        }
    }
}
